<template>
  <!-- 新增和修改弹框 -->
  <el-dialog title="新增/修改 " :visible.sync="dialogShow" @close="closeDialog">
    <!-- 绘制表单 -->
    <el-form ref="form" :model="formData" :rules="formRules" label-width="160px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:440px" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:440px" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <!-- <el-input v-model="formData.manager" style="width:440px" /> -->
        <el-select v-model="formData.manager" style="width:440px" placeholder="请选择" @focus="getEmployeeData">
          <el-option v-for="(item,index) in managerList " :key="index" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" type="textarea" style="width:440px" rows="3" />
        <el-row type="flex" justify="center">
          <el-button size="small" type="primary" @click="addBtn">确定</el-button>
          <el-button size="small" @click="dialogShow = false">取消</el-button>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { getDepartmentListApi, addDepartmentApi, getDepartmentInfoApi, editDepartmentApi } from '@/api/departments'
import { getEmployeeSimpleApi } from '@/api/employees'
export default {
  data() {
    // 部门名称的校验
    const checkNameRepeat = async (valid, value, cb) => {
      // 找到部门名称
      const { depts } = await getDepartmentListApi()
      //   console.log(depts)
      // 找到兄弟部门
      let arr = depts.filter(item => item.id === this.formData.pid)
      // 如果是编辑状态，还要排除自己
      if (this.formData.id) {
        arr = arr.filter(item => item.id !== this.formData.id)
      }
      // 名称查重
      const isRepeat = arr.some(item => item.name === value)
      isRepeat ? cb(new Error('名称重复了')) : cb()
    }
    // 编码校验
    const checkCodeRepeat = async (valid, value, cb) => {
      let { depts } = await getDepartmentListApi()
      console.log(this.formData.id)
      // 编辑状态，完成自己
      if (this.formData.id) {
        depts = depts.filter(item => item.id !== this.formData.id)
      }
      const isRepeat = depts.some(item => item.code === value)
      isRepeat ? cb(new Error('编码重复了')) : cb()
    }
    return {
      dialogShow: false, // 弹窗控制变量
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '', // 部门介绍
        pid: ''// 父级部门Id
      },
      // 负责人数据
      managerList: [],
      // 定义校验规则
      formRules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { validator: checkNameRepeat, trigger: 'blur' }
        ],
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          { trigger: 'blur', validator: checkCodeRepeat }
        ],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    async getEmployeeData() {
      this.managerList = await getEmployeeSimpleApi()
    },
    async addBtn() {
      // 表单数据校验
    //   console.log(this.$refs.form)
      try {
        await this.$refs.form.validate()
      } catch (error) {
        return console.log(error)
      }

      if (this.formData.id) {
        await editDepartmentApi(this.formData)
        this.$message.success('修改成功')
      } else {
        // 发出请求
        await addDepartmentApi(this.formData)
        // 提示成功
        this.$message.success('添加成功')
      }
      // 更新列表
      this.$emit('updateList')
      // 关闭弹窗，重置表单
      this.dialogShow = false
    },
    // 关闭弹窗
    closeDialog() {
      this.formData = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '', // 部门介绍
        pid: ''// 父级部门Id
      }
      // 移除校验
      this.$refs.form.clearValidate()
    },
    // 获取回显数据
    async getFormDataInfo(Id) {
      const res = await getDepartmentInfoApi(Id)
      console.log(res)
      this.formData = res
      this.dialogShow = true
    }
  }
}
</script>

<style>

</style>
